﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using LynxWiki.Search;
using LynxWiki.DomainModel;
using LynxWiki.Filters;
using LynxWiki.Repository;

namespace LynxWiki.Controllers
{
    [InitializeSimpleMembership]
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Index(string searchTerm = "")
        {
            ViewBag.Application = "Version: " + Assembly.GetExecutingAssembly().GetName().Version;
            ViewBag.TagCloud = new TagCloud(LuceneSearch.GetTagCount());
            ViewBag.WikiFlds = BuildWikiSrchFlds("");
            ViewBag.AttachFlds = BuildAttachmentSrchFlds("");
            List<SearchData> sdl = new List<SearchData>();
            if (!string.IsNullOrEmpty(searchTerm)) {
                sdl = LuceneSearch.Search(searchTerm).ToList();
            }
            return View(sdl);
        }

        [HttpPost]
        [TopicActionFilter]
        public ActionResult Index(FormCollection fc)
        {
            ViewBag.SrchTerm = "";
            ViewBag.SrchFld = "";
            List<SearchData> sdl = new List<SearchData>();
            if (fc["toggler"] == "1") {
                if (!string.IsNullOrEmpty(fc["txtSrchTerm"]) && !string.IsNullOrEmpty(fc["txtSrchFld"])) {
                    ViewBag.SrchFld = fc["txtSrchFld"];
                    ViewBag.SrchTerm = fc["txtSrchTerm"];
                    sdl = LuceneSearch.Search(fc["txtSrchTerm"], fc["txtSrchFld"]).ToList();

                }
                else if (!string.IsNullOrEmpty(fc["txtSrchTerm"])) {
                    ViewBag.SrchTerm = fc["txtSrchTerm"];
                    sdl = LuceneSearch.Search(fc["txtSrchTerm"]).ToList();
                }
            }
            else {
                if (!string.IsNullOrEmpty(fc["txtSrchTerm"]) && !string.IsNullOrEmpty(fc["attSrchFld"])) {
                    ViewBag.SrchFld = fc["attSrchFld"];
                    ViewBag.SrchTerm = fc["txtSrchTerm"];
                    sdl = AttachmentSearch.Search(fc["txtSrchTerm"], fc["attSrchFld"]).ToList();

                }
                else if (!string.IsNullOrEmpty(fc["txtSrchTerm"])) {
                    ViewBag.SrchTerm = fc["txtSrchTerm"];
                    sdl = AttachmentSearch.Search(fc["txtSrchTerm"]).ToList();
                }
            }
            ViewBag.WikiFlds = BuildWikiSrchFlds(fc["txtSrchTerm"]);
            ViewBag.AttachFlds = BuildAttachmentSrchFlds(fc["attSrchTerm"]);
            return View(sdl);
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult WikiOptimizeIndex()
        {
            LuceneSearch.Optimize();
            return RedirectToAction("WikiIndexStatus");
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult WikiClearIndex()
        {
            LuceneSearch.ClearLuceneIndex();
            return RedirectToAction("WikiIndexStatus");
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult WikiRebuildIndex()
        {
            WikiDirectoryRepository wikiDirRep = new WikiDirectoryRepository(Server.MapPath(@"~\" + WikiRoot.RootDirectory));
            wikiDirRep.IndexDirectoryFiles();
            IEnumerable<WikiDirectory> wdl = wikiDirRep.GetSubDirectoryList();
            foreach (WikiDirectory dir in wdl) {
                WikiDirectoryRepository wdr = new WikiDirectoryRepository(Server.MapPath(@"~\" + WikiRoot.RootDirectory + @"\" + dir.DirectoryName));
                wdr.IndexDirectoryFiles();
            }
            return RedirectToAction("WikiIndexStatus");
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult WikiIndexStatus()
        {
            LynxWiki.DomainModel.IndexStatus status = LuceneSearch.CheckIndexStatus();
            return View("IndexStatus", status);
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult AttOptimizeIndex()
        {
            AttachmentSearch.Optimize();
            return RedirectToAction("AttIndexStatus");
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult AttClearIndex()
        {
            AttachmentSearch.ClearLuceneIndex();
            return RedirectToAction("AttIndexStatus");
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult AttRebuildIndex()
        {
            //WikiDirectoryRepository wikiDirRep = new WikiDirectoryRepository(Server.MapPath(@"~\" + WikiRoot.RootDirectory));
            //wikiDirRep.IndexDirectoryFiles();
            //IEnumerable<WikiDirectory> wdl = wikiDirRep.GetSubDirectoryList();
            //foreach (WikiDirectory dir in wdl) {
            //    WikiDirectoryRepository wdr = new WikiDirectoryRepository(Server.MapPath(@"~\" + WikiRoot.RootDirectory + @"\" + dir.DirectoryName));
            //    wdr.IndexDirectoryFiles();
            //}
            AttachmentNamespaceRepository anr = new AttachmentNamespaceRepository();
            List<AttachmentNamespace> anl = anr.GetAllNamespaceNames();
            foreach (AttachmentNamespace ns in anl) {
                anr = new AttachmentNamespaceRepository(ns.Namespace, User);
                anr.IndexNamespaceFiles();
            }

            return RedirectToAction("AttIndexStatus");
        }

        [Authorize(Roles = "WikiAdmin")]
        [TopicActionFilter]
        public ActionResult AttIndexStatus()
        {
            LynxWiki.DomainModel.IndexStatus status = AttachmentSearch.CheckIndexStatus();
            return View("IndexStatus", status);
        }

        private List<SelectListItem> BuildWikiSrchFlds(string filter)
        {
            return new List<SelectListItem>() {
                new SelectListItem() { Value = "TopicName", Text = "TopicName", Selected = (filter == "TopicName" ? true : false) },
                new SelectListItem() { Value = "WikiDir", Text = "WikiDir", Selected = (filter == "WikiDir" ? true : false) },
                new SelectListItem() { Value = "Creator", Text = "Creator", Selected = (filter == "Creator" ? true : false) },
                new SelectListItem() { Value = "WikiText", Text = "WikiText", Selected = (filter == "WikiText" ? true : false) },
                new SelectListItem() { Value = "Tags", Text = "Tags", Selected = (filter == "Tags" ? true : false) }
            };
        }

        private List<SelectListItem> BuildAttachmentSrchFlds(string filter)
        {
            return new List<SelectListItem>() {
                new SelectListItem() { Value = "AttachmentName", Text = "AttachmentName", Selected = (filter == "AttachmentName" ? true : false) },
                new SelectListItem() { Value = "AttachmentDir", Text = "AttachmentDir", Selected = (filter == "AttachmentDir" ? true : false) },
                new SelectListItem() { Value = "Creator", Text = "Creator", Selected = (filter == "Creator" ? true : false) },
                new SelectListItem() { Value = "NameSpace", Text = "NameSpace", Selected = (filter == "NameSpace" ? true : false) },
                new SelectListItem() { Value = "AttachmentText", Text = "AttachmentText", Selected = (filter == "AttachmentText" ? true : false) },
                new SelectListItem() { Value = "Tags", Text = "Tags", Selected = (filter == "Tags" ? true : false) }
            };
        }
    }
}
